/*
 * Copyright 2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.kommando.core.command.objectsource;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.Icon;

import org.kommando.core.action.Action;
import org.kommando.core.action.ActionRepository;
import org.kommando.core.catalog.AbstractObjectSource;
import org.kommando.core.catalog.Catalog;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.core.catalog.SimpleCatalogObject;
import org.kommando.core.command.Command;
import org.kommando.core.selector.ActionCommand;
import org.kommando.core.util.Assert;

/**
 * @author Peter De Bruycker
 */
public class CommandObjectSource extends AbstractObjectSource {

    private Catalog catalog;
    private ActionRepository actionRepository;

    public CommandObjectSource(Catalog catalog, ActionRepository actionRepository) {
        Assert.argumentNotNull("catalog", catalog);
        Assert.argumentNotNull("actionRepository", actionRepository);

        this.catalog = catalog;
        this.actionRepository = actionRepository;
    }

    @Override
    public String getId() {
        return "commands";
    }

    @Override
    public String getDescription() {
        return "Commands";
    }

    @Override
    public Icon getIcon() {
        return null;
    }

    @Override
    public String getName() {
        return "Commands";
    }

    @Override
    public CatalogObject getObject(String id) {
        if (id.startsWith("command:")) {
            String commandId = id.substring("command:".length());

            Pattern p = Pattern.compile("\\(([^\\)]*)\\)\\(([^\\)]*)\\)(?:\\(([^\\)]*)\\))?");
            Matcher matcher = p.matcher(commandId);
            if (matcher.matches()) {
                CatalogObject direct = catalog.getObject(matcher.group(1));
                if (direct == null) {
                    return null;
                }
                Action action = actionRepository.getAction(matcher.group(2));
                if (action == null) {
                    return null;
                }

                CatalogObject indirect = null;
                if (matcher.group(3) != null) {
                    indirect = catalog.getObject(matcher.group(3));
                    if (indirect == null) {
                        return null;
                    }
                }

                Command command = new ActionCommand(direct, action, indirect);

                SimpleCatalogObject commandObject = new SimpleCatalogObject(command.getId(), command.getName(), command.getDescription(), command.getIcon());
                commandObject.setExtension(Command.class, command);

                return commandObject;
            }
        }

        return null;
    }
}
